<?php


namespace app\service;

use app\model\Result;
use app\utils\Datetime;
use think\facade\Db;
use think\facade\Request;

/**
 * 商品
 */
class GoodsService
{
    /**
     * 根据商品名称搜索类似商品
     */
    public static function queryGoodsByGoods($param) {

        //常规(系列)
        if($param['type'] == '1'){
            $where1 = [];
            $where2 = [];
            $where3 = [];
            $goods = $param['goods'];
            if($param['goods']){
                $where1[] = ['goods','like','%'.$goods.'%'];
                $where2[] = ['father_category','like','%'.$goods.'%'];
                $where3[] = ['son_category','like','%'.$goods.'%'];
            }
            $res = Db::name('goods')->field('*')->where($where1)->whereOr($where2)->whereOr($where3)->where('status',1)->limit($param['startPage'],$param['endPage'])->select();
            return Result::index(200,'操作成功',$res);
        }

        //价格(升序)
        if($param['type'] == '2'){
            $where1 = [];
            $where2 = [];
            $where3 = [];
            $goods = $param['goods'];
            if($param['goods']){
                $where1[] = ['goods','like','%'.$goods.'%'];
                $where2[] = ['father_category','like','%'.$goods.'%'];
                $where3[] = ['son_category','like','%'.$goods.'%'];
            }
            $res = Db::name('goods')->field('*')->where($where1)->whereOr($where2)->whereOr($where3)->where('status',1)->order('price asc')->limit($param['startPage'],$param['endPage'])->select();
            return Result::index(200,'操作成功',$res);
        }

        //价格(降序)
        if($param['type'] == '3'){
            $where1 = [];
            $where2 = [];
            $where3 = [];
            $goods = $param['goods'];
            if($param['goods']){
                $where1[] = ['goods','like','%'.$goods.'%'];
                $where2[] = ['father_category','like','%'.$goods.'%'];
                $where3[] = ['son_category','like','%'.$goods.'%'];
            }
            $res = Db::name('goods')->field('*')->where($where1)->whereOr($where2)->whereOr($where3)->where('status',1)->order('price desc')->limit($param['startPage'],$param['endPage'])->select();
            return Result::index(200,'操作成功',$res);
        }

        //付款数
        if($param['type'] == '4'){
            $where1 = [];
            $where2 = [];
            $where3 = [];
            $goods = $param['goods'];
            if($param['goods']){
                $where1[] = ['goods','like','%'.$goods.'%'];
                $where2[] = ['father_category','like','%'.$goods.'%'];
                $where3[] = ['son_category','like','%'.$goods.'%'];
            }
            $res = Db::name('goods')->field('*')->where($where1)->whereOr($where2)->whereOr($where3)->where('status',1)->order('pay_num desc')->limit($param['startPage'],$param['endPage'])->select();
            return Result::index(200,'操作成功',$res);
        }

        //新品
        if($param['type'] == '5'){
            $where1 = [];
            $where2 = [];
            $where3 = [];
            $goods = $param['goods'];
            if($param['goods']){
                $where1[] = ['goods','like','%'.$goods.'%'];
                $where2[] = ['father_category','like','%'.$goods.'%'];
                $where3[] = ['son_category','like','%'.$goods.'%'];
            }
            $res = Db::name('goods')->field('*')->where($where1)->whereOr($where2)->whereOr($where3)->where('status',1)->order('create_time desc')->limit($param['startPage'],$param['endPage'])->select();
            return Result::index(200,'操作成功',$res);
        }

        else {
            $where1 = [];
            $where2 = [];
            $where3 = [];
            $goods = $param['goods'];
            if($param['goods']){
                $where1[] = ['goods','like','%'.$goods.'%'];
                $where2[] = ['father_category','like','%'.$goods.'%'];
                $where3[] = ['son_category','like','%'.$goods.'%'];
            }
            $res = Db::name('goods')->field('*')->where($where1)->whereOr($where2)->whereOr($where3)->where('status',1)->limit($param['startPage'],$param['endPage'])->select();
            return Result::index(200,'操作成功',$res);
        }
    }

    /**
     * 根据商品uuid获取商品详情
     */
    public static function queryGoodsByUuid($param) {
        $ch = LoveService::goodsLoveCheck($param);

        $res = Db::name('goods')->field('*')->where('uuid',$param['goods_uuid'])->where('status',1)->find();

        $res['love'] = $ch;

        return Result::index(200,'操作成功',$res);
    }

    /**
     * 新增商品
     */
    public static function createGoods($param) {
        $id = Db::name('goods')->insertGetId($param);

        if (empty($id)) {
            return Result::index(100,'插入失败','');
        }

        return Result::index(200,'插入成功','');
    }

    /**
     * 更新商品信息
     */
    public static function updateGoods($param) {

        $res = Db::name('goods')->where('uuid','=',$param['uuid'])->update([
            'goods' => $param['goods'],
            'address' => $param['address'],
            'price' => $param['price'],
            'goods_num' => $param['goods_num'],
            'img' => $param['img'],
            'son_category' => $param['son_category'],
            'father_category' => $param['father_category'],
            'discount' => $param['discount'],
            'discount_start_time' => $param['discount_start_time'],
            'discount_end_time' => $param['discount_end_time'],
            'status' => $param['status'],
            'goods_level' => $param['goods_level'],
            'parameter' => $param['parameter']
        ]);

        return Result::index(200,'更新成功',$res);
    }

    /**
     * 根据收藏数返回商品列表
     */
    public static function listGoodsByLoveNum($param) {
        $res = Db::name('goods')->field('*')->where('status',1)->order('love_num desc')->limit($param['startPage'],$param['endPage'])->select();
        return Result::index(200,'操作成功',$res);
    }

    /**
     * 根据付款数返回商品列表
     */
    public static function listGoodsByPayNum($param) {
        $res = Db::name('goods')->field('*')->where('status',1)->order('pay_num desc')->limit($param['startPage'],$param['endPage'])->select();
        return Result::index(200,'操作成功',$res);
    }

    /**
     * 根据折扣力度返回打折商品列表
     */
    public static function listGoodsByDiscount($param) {
        $res = Db::name('goods')->field('*')->where('status',1)->where('discount','>',0)->order('discount asc')->limit($param['startPage'],$param['endPage'])->select();
        return Result::index(200,'操作成功',$res);
    }

    /**
     * 根据商品级别返回商品列表
     */
    public static function listGoodsByLevel($param) {
        $res = Db::name('goods')->field('*')->where('status',1)->where('goods_level','=',$param['goods_level'])->limit($param['startPage'],$param['endPage'])->select();
        return Result::index(200,'操作成功',$res);
    }

    /**
     * 根据父类别返回商品列表
     */
    public static function listGoodsByFatherCategory($param) {
        $res = Db::name('goods')->field('*')->where('status',1)->where('father_category',$param['father_category'])->limit($param['startPage'],$param['endPage'])->select();
        return Result::index(200,'操作成功',$res);
    }

    /**
     * 根据子类别返回商品列表
     */
    public static function listGoodsBySonCategory($param) {
        if(!$param['son_category']){
            $res = Db::name('goods')->field('*')->where('status',1)->order('love_num desc')->limit($param['startPage'],$param['endPage'])->select();
            return Result::index(200,'操作成功',$res);
        }
        $res = Db::name('goods')->field('*')->where('status',1)->where('son_category',$param['son_category'])->limit($param['startPage'],$param['endPage'])->select();
        return Result::index(200,'操作成功',$res);
    }
}
